package com.zr.inquiry.controller;

import com.zr.inquiry.meiju.CurrencyCodeEnum;
import com.zr.inquiry.meiju.StatusVo;
import com.zr.inquiry.meiju.TypeEnum;
import com.zr.inquiry.meiju.isTaxEnum;
import com.zr.inquiry.pojo.*;
import com.zr.inquiry.service.InquiryService;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 询价单controller层
 */
@CrossOrigin
@RestController
public class InquiryController {

    @Autowired
    private InquiryService inquiryService;

    //分页
    @PostMapping("queryPage")
    public ResultVo querypage(@RequestBody InquirySelectVo inquirySelectVo){
        return inquiryService.queryPage(inquirySelectVo);
    }

    //修改
    @PostMapping("update")
    public ResultVo update (@RequestBody @Valid InquiryUpdateVo inquiryUpdateVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return  inquiryService.update(inquiryUpdateVo);
    }

    //新增选择备件
    @PostMapping("chakan/bejian")
    public ResultVo chakanddai(@RequestBody SparedetailClassVo sparedetailClassVo){
        return inquiryService.chabeijian(sparedetailClassVo);

    }
    //新增选择供应商
    @PostMapping("chakan/供应商")
    public ResultVo chakandgong(@RequestBody SupplierClassVo supplierClassVo){
        return inquiryService.chagongying(supplierClassVo);

    }

    //下拉框
    @PostMapping("metadata/getPullDownList")
    public ResultVo xiala(@RequestBody List<String> xiala){
        Map<String,List<StatusVo>> xialamap=new HashMap<>();

        //币种
        List<StatusVo> currencyCodeList=new ArrayList<>();
        for (CurrencyCodeEnum c:CurrencyCodeEnum.values()) {
            StatusVo statusVo=new StatusVo();
            statusVo.setLable(c.getStatusValue());
            statusVo.setValue(c.getStatusName());
            currencyCodeList.add(statusVo);
        }
        xialamap.put("currencyCode",currencyCodeList);

        //含税
        List<StatusVo> isTaxList=new ArrayList<>();
        for (isTaxEnum i: isTaxEnum.values()) {
            StatusVo statusVo=new StatusVo();
            statusVo.setLable(i.getStatusValue());
            statusVo.setValue(i.getStatusName());
            isTaxList.add(statusVo);
        }
        xialamap.put("isTax",isTaxList);

        //类型
        List<StatusVo> typeList=new ArrayList<>();
        for (TypeEnum t: TypeEnum.values()) {
            StatusVo statusVo=new StatusVo();
            statusVo.setLable(t.getStatusValue());
            statusVo.setValue(t.getStatusName());
            typeList.add(statusVo);
        }
        xialamap.put("type",typeList);

        Map<String,List<StatusVo>> newxialaMap = new HashMap<>();
        for (String key:xiala){
            newxialaMap.put(key, xialamap.get(key));
        }
        return ResultVoBuilder.success(newxialaMap);


    }

    //添加
    @PostMapping("inquiryList/add")
    public ResultVo add(@RequestBody @Valid InquiryAddVo inquiryAddVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return ResultVoBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return inquiryService.add(inquiryAddVo);

    }

    @GetMapping("/queryById")
    public ResultVo queryById(Integer id){

        return  inquiryService.queryById(id);
    }

}
